<template>
  <el-breadcrumb class="app-breadcrumb" separator="/">
    <el-breadcrumb-item v-for="(item,index) in levelList" :key="item.title" v-if="item.title">
      <span v-if="index === levelList.length-1" class="no-redirect">{{item.title}}</span>
      <router-link v-else :to="{name: item.name}">{{item.title}}</router-link>
    </el-breadcrumb-item>
  </el-breadcrumb>
</template>

<script>
  export default {
    name: 'BreadCrumb',
    created () {
      this.getBreadcrumb()
    },
    data () {
      return {
        levelList: null
      }
    },
    watch: {
      $route () {
        this.getBreadcrumb()
      }
    },
    methods: {
      getBreadcrumb () {
        // 主页到当前页面中的页面路由信息
        let _meta = this.$route.meta
        // 当前页面路由信息
        let currentRoute = {title: _meta.title}
        // 主页面 + 主页面到当前页面中的页面 + 当前页面
        this.levelList = [{name: 'Index', title: '工作台'}].concat(_meta.prevRouter || [], currentRoute)
        console.log(this.levelList)
      }
    }
  }
</script>

<style lang="less" scoped>
  .app-breadcrumb.el-breadcrumb {
    display: inline-block;
    font-size: 14px;
    line-height: 50px;
    .no-redirect {
      color: #97a8be;
      cursor: text;
    }
  }
</style>
